package com.yksj.healthtalk.ui.interestwall;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import org.handmark.pulltorefresh.library.PullToRefreshListView;
import org.universalimageloader.core.ImageLoader;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.yksj.healthtalk.entity.BaseInfoEntity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartControlClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.FriendHttpUtil;
import com.yksj.healthtalk.utils.SalonHttpUtil;

/**
 * 转发给好友和转发到话题的fragment
 * @author crj
 *
 */
public class TransmitFragment extends Fragment {
	
	private PullToRefreshListView mListView;
	/**
	 * 值为topic 或者 friend 表示是获取转发到话题还是好友的列表
	 */
	private String tag; //话题2,好友1
//	private List<Parcelable> metaData;
	private ImageLoader mImageLoader;
	//private int pageCount = 1;
	private TransmitAdapter adapter;
	
	public static TransmitFragment newInstance(String tag) {
		TransmitFragment f = new TransmitFragment();
		Bundle mBundle = new Bundle();
		mBundle.putString("tag", tag);
		f.setArguments(mBundle);
		return f;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		tag = getArguments().getString("tag");
		
		mImageLoader = ImageLoader.getInstance();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.transmit_fragment_layout, container,false);
		mListView =  (PullToRefreshListView) view.findViewById(R.id.tranmitcontent);
		mListView.setMode(Mode.DISABLED);
		
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		/*List list = null;
		AppData appData = HTalkApplication.getAppData();
		if("2".equals(tag)){//话题
			list = appData.getInterestGroupInfoList();
		}else{//好友1
			list = appData.getInterestFriendList();
		}
		adapter = new TransmitAdapter(getLayoutInflater(savedInstanceState),list);
		mListView.setAdapter(adapter);*/
		
		adapter = new TransmitAdapter(getLayoutInflater(savedInstanceState),new ArrayList());
		mListView.setAdapter(adapter);
		
		RequestParams params = new RequestParams();
		params.put("TYPE",tag);
		params.put("CUSTOMERID", SmartControlClient.getControlClient().getUserId());
		HttpRestClient.doHttpFindInitialize132(params, new ObjectHttpResponseHandler(getActivity()) {
			@Override
			public Object onParseResponse(String content) {
				if("2".equals(tag)){
					//好友1
					return FriendHttpUtil.jsonAnalysisFriendEntity(content,false);
					//list = appData.getInterestGroupInfoList();
				}else{
					return SalonHttpUtil.jsonAnalysisSalonEntity(content);
					//list = appData.getInterestFriendList();
				}
			}
			@Override
			public void onSuccess(int statusCode, Object response) {
				adapter.onChangeData((List)response);
			}
		});
	}
	
	public List<BaseInfoEntity> getSelectedList(){
		return  adapter.mSelectedList;
	}
	
	/**
	 * 构造以逗号分隔的一连串用户id
	 * @return
	 */
	public String getCusIds() {
		if(adapter.mSelectedList.size() == 0)return null;
		List list = adapter.mSelectedList;
		StringBuffer buffer = new StringBuffer();
		for (Object object : list) {
			BaseInfoEntity entity = (BaseInfoEntity)object;
			String id = entity.getId();
			buffer.append(id);
			buffer.append(",");
		}
		int index = buffer.lastIndexOf(",");
		return buffer.substring(0,index);
	}
	
	
	public class TransmitAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		final List mList;
		final List mSelectedList = new ArrayList();
		public TransmitAdapter(LayoutInflater inflater,List list) {
			this.mInflater = inflater;
			this.mList = list;
		}
		@Override
		public int getCount() {
			return mList.size();
		}
		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		public void onChangeData(List list){
			if(list == null || list.size() == 0) return;
			this.mList.addAll(list);
			notifyDataSetChanged();
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			
			if(convertView==null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.transmit_fragment_item, parent,false);
				holder.mHeadImageView = (ImageView) convertView.findViewById(R.id.transmitImageContent);
				holder.mNameTextView = (TextView) convertView.findViewById(R.id.tranmitTextContent);
				holder.mImageView2 = (ImageView) convertView.findViewById(R.id.transmitLevel);
				holder.mImageView3 = (ImageView) convertView.findViewById(R.id.transmitSex);
				holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.ifTransmit);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			final Object object = getItem(position);
			if(object instanceof CustomerInfoEntity){//客户
				setCustomerView(holder,(CustomerInfoEntity)object);
			}else if(object instanceof GroupInfoEntity){//话题
				setGroupView(holder,(GroupInfoEntity)object);
			}
			holder.mCheckBox.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					boolean isChecked = ((CheckBox)v).isChecked();
					if(isChecked){
						mSelectedList.add(object);
					}else{
						mSelectedList.remove(object);
					}
				}
			});
			return convertView;
		}

		public void setGroupView(ViewHolder holder,GroupInfoEntity entity) {
			holder.mNameTextView.setText(entity.getName());
			mImageLoader.displayImage("4",entity.getNormalHeadIcon(),holder.mHeadImageView);
			//话题等级图片初始化
			String str = entity.getGroupLevel();
			switch(Integer.parseInt(str)) {
			case 0:
			case 1:
				holder.mImageView2.setImageResource(R.drawable.v1);
				break;
			case 2:
				holder.mImageView2.setImageResource(R.drawable.v2);
				break;
			case 3:
				holder.mImageView2.setImageResource(R.drawable.v3);
				break;
			case 4:
				holder.mImageView2.setImageResource(R.drawable.v4);
				break;
			case 5:
				holder.mImageView2.setImageResource(R.drawable.v5);
				break;
			}
			//1-医师 2-普通 话题类型
			if(entity.isSalon()) {
				holder.mImageView3.setImageResource(R.drawable.ic_cross);
			} else {
				holder.mImageView3.setImageResource(R.color.transparent);
			}
			holder.mCheckBox.setChecked(mSelectedList.contains(entity));
		}
		
		public void setCustomerView(ViewHolder holder,CustomerInfoEntity entity) {
			holder.mNameTextView.setText(entity.getName());
			mImageLoader.displayImage(entity.getSex(),entity.getNormalHeadIcon(),holder.mHeadImageView);
			
			int roldid = entity.getRoldid();
			
			//用户类型
			if(888 == roldid) {
				holder.mImageView2.setImageResource(R.drawable.v);
			} else {
				holder.mImageView2.setImageResource(R.color.transparent);
			}
			//用户性别 1-男 2-女  0-保密
			String sex = entity.getSex();
			if("1".equals(sex)) {
				holder.mImageView3.setImageResource(R.drawable.man);
			} else if("2".equals(sex)){
				holder.mImageView3.setImageResource(R.drawable.women);
			}
			holder.mCheckBox.setChecked(mSelectedList.contains(entity));
		}
		
		private class ViewHolder {
			TextView mNameTextView;
			ImageView mHeadImageView;
			ImageView mImageView2;
			ImageView mImageView3;
			CheckBox mCheckBox;
		}
		
	}
	
	
}
